﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SlimDX;
using SlimDX.Direct3D10;

namespace SCS.GFX
{
    class RefGrid
    {
        private SlimDX.Direct3D10.Buffer FVertexBuffer = null;


        private static System.Drawing.Color SmallGridColor = System.Drawing.Color.BlueViolet;
        private static System.Drawing.Color LargeGridColor = System.Drawing.Color.Brown;
        private static System.Drawing.Color XColor = System.Drawing.Color.Red;
        private static System.Drawing.Color YColor = System.Drawing.Color.Green;
        private static System.Drawing.Color ZColor = System.Drawing.Color.Blue;

        private Effect FEffect;
        private EffectTechnique FTechnique;
        private EffectPass FPass;
        private InputLayout FLayout;
        public EffectVariable FEffectVariable; //A pointer to the shader, used to update the shaders constants

        public EffectMatrixVariable FEffectViewProjectionMatrix; //A pointer to the shaders VIEWPROJECTION matrix
        public EffectMatrixVariable FEffectWorldMatrix;    //A pointer to the shaders World matrix

        public RefGrid()
        {
            FEffect = Effect.FromFile(Renderer.Device, "..\\..\\..\\..\\Effects\\PositionColored.fx", "fx_4_0", ShaderFlags.None, EffectFlags.None, null, null);
            FTechnique = FEffect.GetTechniqueByIndex(0);
            //Get the first pass
            FPass = FTechnique.GetPassByIndex(0);

            FLayout = new InputLayout(Renderer.Device, PositionColored.InputElements, FPass.Description.Signature);

            FEffectVariable = FEffect.GetVariableByName("ViewProjection");
            FEffectViewProjectionMatrix = FEffectVariable.AsMatrix();

            FEffectVariable = FEffect.GetVariableByName("World");
            FEffectWorldMatrix = FEffectVariable.AsMatrix();


            PositionColored[] Grid = new PositionColored[206];

            Vector4 CurrentColor;

            int GridSize = 100;
            int HalfGrid = GridSize / 2;
            float GridExtent = (float)HalfGrid;

            uint Vertex = 0;

            for (int i = 0; i < 101; i += 2)
            {
                float x;
                x = (float)i - 50.0f;


                if (i == HalfGrid)
                {
                    CurrentColor = new Vector4(1.0f,0.0f,0.0f,1.0f);
                }
                else if (i % 5 == 0)
                {
                    CurrentColor = new Vector4(1.0f, 1.0f, 0.0f, 1.0f);
                }
                else
                {
                    CurrentColor = new Vector4(1.0f, 0.0f, 1.0f, 1.0f);
                }

                Grid[Vertex] = new PositionColored(-GridExtent, 0f, x, CurrentColor);
                Vertex++;
                Grid[Vertex] = new PositionColored(GridExtent, 0f, x, CurrentColor);
                Vertex++;
            }

            for (int i = 0; i < 101; i += 2)
            {
                float y;

                y = (float)i - 50.0f;

                if (i == HalfGrid)
                {
                    CurrentColor = new Vector4(0.0f, 1.0f, 0.0f, 1.0f);
                }
                else if (i % 5 == 0)
                {
                    CurrentColor = new Vector4(1.0f, 1.0f, 0.0f, 1.0f);
                }
                else
                {
                    CurrentColor = new Vector4(1.0f, 0.0f, 1.0f, 1.0f);
                }

                Grid[Vertex] = new PositionColored(y, 0f, -GridExtent, CurrentColor);
                Vertex++;
                Grid[Vertex] = new PositionColored(y, 0f, GridExtent, CurrentColor);
                Vertex++;
            }


            CurrentColor = new Vector4(0.0f, 0.0f, 1.0f, 1.0f);
            Grid[Vertex] = new PositionColored(0f, -GridExtent, 0f, CurrentColor);
            Vertex++;
            Grid[Vertex] = new PositionColored(0f, GridExtent, 0f, CurrentColor);
            Vertex++;


            int FVerticeCount = 206;

            //Construct vertex buffer for drawing
            BufferDescription bufferDescription = new BufferDescription();
            bufferDescription.BindFlags = BindFlags.VertexBuffer;
            bufferDescription.CpuAccessFlags = CpuAccessFlags.None;
            bufferDescription.OptionFlags = ResourceOptionFlags.None;
            bufferDescription.SizeInBytes = FVerticeCount * PositionColored.SizeInBytes;
            bufferDescription.Usage = ResourceUsage.Default;


            DataStream stream = new DataStream(FVerticeCount * PositionColored.SizeInBytes, true, true);
            stream.Position = 0;

            foreach (PositionColored i in Grid)
            {
                stream.Write(new Vector3(i.X, i.Y, i.Z));
                stream.Write(i.Color);
            }

            stream.Position = 0;

            FVertexBuffer = new SlimDX.Direct3D10.Buffer(Renderer.Device, stream, bufferDescription);
            stream.Close();

            //FVertexBuffer = VertexHelper.CreateBuffer(aDevice, Grid);

        }

        public void Render(RenderInfo info)
        {
            //Set input layout and primitive topology
            Renderer.Device.InputAssembler.SetInputLayout(FLayout);
            Renderer.Device.InputAssembler.SetPrimitiveTopology(PrimitiveTopology.LineList);

            //Set vertex and index buffers
            Renderer.Device.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(FVertexBuffer, PositionColored.SizeInBytes, 0));
            //Renderer.Device.InputAssembler.SetIndexBuffer(FIndexBuffer, SlimDX.DXGI.Format.R16_UInt, 0);

//            FWorldMatrix = FObjectMatrix * Matrix.Translation(FPosition) * Matrix.Translation(FLocation);

            FEffectWorldMatrix.SetMatrix(Matrix.Identity);
            FEffectViewProjectionMatrix.SetMatrix(Renderer.View * Renderer.Projection);


                FPass.Apply();
                Renderer.Device.Draw(206, 0);
            

        }

    }
}
